<?php
namespace Admin\Controller;
use Think\Controller;
header('content-type:text/html;charset="UTF-8"');
class ProductController extends CommonController {
    public function index(){
        $this->display();
    }
    public function up(){
       
    	if ($_FILES['img']['name']!=false) {
    		$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =     'Public/Upload/'; // 设置附件上传根目录
			$upload->savePath  =     ''; // 设置附件上传（子）目录
			// 上传文件 
			$info   =   $upload->upload();
			
			if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
			}else{// 上传成功
				$img="/Public/Upload/{$info[0]['savepath']}{$info[0]['savename']}";
				echo $img;
			}
    	}
    }

    public function add(){
        
    	$id=I('get.id');
        $images=I('get.images');

        $db=M('product');
        if ($id==false) {           
            $data=$db->create();
            $data['detail']=I('post.detail','','');
            $data['images']=I('post.images');
            $data['images']=json_encode($data['images']);
            $ret=$db->add($data);
            if ($ret!=false) {
               $this->success("添加成功","/Admin/Product/edit",2);
            }
        } else {
            if ($_POST['images']!=false) {
                $data['images']=I('post.images');
                $data['images']=json_encode($data['images']);
            }
            $data['title']=I('post.title');
            $data['price']=I('post.price');
            $data['class']=I('post.class');
            $data['originprice']=I('post.originprice');
            $data['count']=I('post.count');
            $data['detail']=I('post.detail','','');
            
           
            $ret=$db->where("id=$id")->save($data);
            $this->success("修改成功","/Admin/Product/edit",2);
        }
    }
    	
   
    public function edit(){
        $db=M('product');
        $data=$db->select();
        foreach ($data as $key => $value) {
            $value['images']=json_decode($value['images'],true);
            $data[$key]['images']=$value['images'][0];
        }
        $this->assign('data',$data);
        $this->display();
    }
    public function change (){
        $id=I('get.id');
        $db=M('product');
        $data=$db->where("id={$id}")->find();
        $data['images']=json_decode($data['images'],true);
        $this->assign('data',$data);
        $this->display('index');
    }
    public function delete(){
        $id=I('get.id');
        $db=M('product');
        $db->where("id={$id}")->delete();
        $this->success("删除成功","",2);
    }	  
}